/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.profile;

import org.openaecfm.model.profile.ParameterizedProfileDef;
import org.openaecfm.model.profile.ZShapeProfileDef;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class ZShapeProfileDefIO extends
		AbstractEntityInstanceInputImpl<ZShapeProfileDef> implements
		IEntityInstanceOutput<ZShapeProfileDef> {
	private IEntityInstanceUtilInput<ParameterizedProfileDef> utilInput;
	private IEntityInstanceUtilOutput<ParameterizedProfileDef> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<ParameterizedProfileDef> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(ZShapeProfileDef pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCZSHAPEPROFILEDEF", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set Depth
		RealParameter depth = new RealParameter(pojo.getDepth());
		ei.addParameter(depth);

		// Set FlangeWidth
		RealParameter flangewidth = new RealParameter(pojo.getFlangeWidth());
		ei.addParameter(flangewidth);

		// Set WebThickness
		RealParameter webthickness = new RealParameter(pojo.getWebThickness());
		ei.addParameter(webthickness);

		// Set FlangeThickness
		RealParameter flangethickness = new RealParameter(
				pojo.getFlangeThickness());
		ei.addParameter(flangethickness);

		// Set FilletRadius
		if (pojo.getFilletRadius() == null) {
			DollarParameter d = DollarParameter.getInstance();
			ei.addParameter(d);
		} else {
			RealParameter r = new RealParameter(pojo.getFilletRadius());
			ei.addParameter(r);
		}

		// Set EdgeRadius
		if (pojo.getEdgeRadius() == null) {
			DollarParameter d = DollarParameter.getInstance();
			ei.addParameter(d);
		} else {
			RealParameter r = new RealParameter(pojo.getEdgeRadius());
			ei.addParameter(r);
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ZShapeProfileDef doCreateObject() {
		return new ZShapeProfileDef();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, ZShapeProfileDef pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<ParameterizedProfileDef> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			ZShapeProfileDef pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Depth
		RealParameter depth = (RealParameter) sei.getParameter(3);
		pojo.setDepth(depth.toDouble());

		// Set FlangeWidth
		RealParameter flangewidth = (RealParameter) sei.getParameter(4);
		pojo.setFlangeWidth(flangewidth.toDouble());

		// Set WebThickness
		RealParameter webthickness = (RealParameter) sei.getParameter(5);
		pojo.setWebThickness(webthickness.toDouble());

		// Set FlangeThickness
		RealParameter flangethickness = (RealParameter) sei.getParameter(6);
		pojo.setFlangeThickness(flangethickness.toDouble());

		// Set FilletRadius
		Parameter filletradius = sei.getParameter(7);
		if (filletradius.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) filletradius;
			pojo.setFilletRadius(r.toDouble());
		}

		// Set EdgeRadius
		Parameter edgeradius = sei.getParameter(8);
		if (edgeradius.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) edgeradius;
			pojo.setEdgeRadius(r.toDouble());
		}
	}
}
